﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Google.GData.Photos;
using Google.GData.Extensions.MediaRss;
using Microsoft.MediaCenter.UI;

#pragma warning disable 3001, 3002, 3003
namespace McePicasa
{
    public class PicasaPhoto : PicasaObject
    {

        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "3#")]
        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "4#")]
        public PicasaPhoto(PicasaAlbum owner, PhotoAccessor photoAccessor, string title, string photoUrl, string thumbnailUrl)
        {
            this.owner = owner;
            this.photoAccessor = photoAccessor;
            MyId = photoAccessor.Id;
            MyTitle = title;
            MyUrl = photoUrl;
            this.MyThumbnailUrl = thumbnailUrl;
        }

        public PicasaAlbum Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public PicasaPhoto PrevPhoto
        {
            get { return owner == null ? null : owner.GetPrevPhoto(this); }
        }

        public PicasaPhoto NextPhoto
        {
            get { return owner == null ? null : owner.GetNextPhoto(this); }
        }

        public PhotoAccessor PhotoAccessor
        {
            get { return photoAccessor; }
            set { photoAccessor = value; }
        }

        public Image LoadImage(bool thumbnail)
        {
            string ext;
            string filename = Application.Current.AppDataPath() + @"\";

            try
            {
                filename += (owner as PicasaAlbum).Owner.Id2;
                if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);

                filename += @"\";
                filename += (owner as PicasaAlbum).AlbumAccessor.Id;
                if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);

                if (thumbnail)
                {
                    filename += @"\thumbnails";
                    ext = (MyThumbnailUrl != null) && (MyThumbnailUrl.Length > 3) ?
                        MyThumbnailUrl.Substring(MyThumbnailUrl.Length - 3) : "jpg";
                    if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);
                }
                else
                {
                    ext = (MyUrl != null) && (MyUrl.Length > 3) ?
                    MyUrl.Substring(MyUrl.Length - 3) : "jpg";
                }

                filename += @"\";
                filename += (MyId + "." + ext);
                if (!File.Exists(filename))
                {
                    try
                    {
                        if (!PicasaAPI.SaveImage(thumbnail ? MyThumbnailUrl : MyUrl + "?imgmax=" + Global.Imgmax, filename))
                            filename = null;
                    }
                    catch (PicasaException e)
                    {
                        Log.LogIt("PicasaPhoto.LoadImage: " + e.Message);
                        filename = null;
                    }
                }

                downloadFileSize = (int)(new System.IO.FileInfo(filename)).Length;
            }
            catch (Exception e)
            {
                filename = null;
                Log.LogIt(e);
            }
            //Log.LogIt("LoadImage returning: " + (filename == null ? "null" : filename));
            return (filename != null) ? new Image("file://" + filename) : null;
        }

        public int DownloadFileSize
        {
            get { return downloadFileSize; }
            set { downloadFileSize = value; }
        }

        private PicasaAlbum owner = null;
        private PhotoAccessor photoAccessor = null;
        private int downloadFileSize;
    }
}
